package com.software.usercenter.controller;

import cn.hutool.json.JSONObject;
import com.software.usercenter.domain.Result;
import com.software.usercenter.entity.*;
import com.software.usercenter.exception.BizException;
import com.software.usercenter.feignclient.QuestionCenterFeignClient;
import com.software.usercenter.judge.JudgeMethod;
import com.software.usercenter.judge.Task;
import com.software.usercenter.service.AdminService;
import com.software.usercenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : skyzo 11917
 * @date : 2023/4/24
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private QuestionCenterFeignClient questionCenterFeignClient;
    @Autowired
    private Task task;

    //登录的方法
    @GetMapping("/login")
    public Result login(User user){
        User login = adminService.login(user);
        //返回回来说明没有抛异常 登录成功
        return ResultUtil.success("200","登录成功",login);
    }
    //发布题目的方法
    @PostMapping("/publishQuestion")
    public Result publishQuestion(@RequestBody Question question){
        adminService.publishQuestion(question);
        return ResultUtil.success("200", "题目发布成功", null);
    }

    //删除题目的方法
    @PostMapping("/deleteQuestion")
    public Result deleteQuestion(@RequestBody Question question){
        adminService.deleteQuestion(question);
        return ResultUtil.success("200", "题目删除成功", null);
    }

    //编辑题目的方法
    @PostMapping("/updateQuestion")
    public Result updateQuestion(@RequestBody Question question){
         adminService.updateQuestion(question);
        return ResultUtil.success("200", "题目更新成功",null);
    }

    //给某个用户添加权限
    @PostMapping("/grantPermission/{userId}/{permission}")
    public Result grantPermission(@PathVariable String userId, @PathVariable String permission){
        User userWithNewPermission = adminService.grantPermission(userId, permission);
        return ResultUtil.success("200", "用户权限添加成功", userWithNewPermission);
    }

    //自动判题功能
    @GetMapping("/autoJudgeQuestion/{questionId}")
    public Result autoJudgeQuestion(@PathVariable Integer questionId){
        List<Record> recordList = adminService.autoJudgeQuestion(questionId);
        System.out.println("size : " + recordList.size());
        System.out.println(recordList.toString());
        List<Score> scoreList = new ArrayList<>();
        for (Record record : recordList) {
            // 1.评测代码
            if (record.getStatus().equals("Accepted")) {
                Score score = new Score(record.getUserId(), questionId, 100.0);
                scoreList.add(score);
            } else {
                Integer num = record.getNum();
                if (num != null) {
                    Score score = new Score(record.getUserId(), questionId, num * 10.0);
                    scoreList.add(score);
                } else if (num == null) {
                    Score score = new Score(record.getUserId(), questionId, 0.0);
                    scoreList.add(score);
                }
            }
        }
        System.out.println("score size : " + scoreList.size());
        System.out.println(scoreList.toString());
        return ResultUtil.success(scoreList);
    }
}








